import Local_Window_ItemCategory from '@swda/Scene/ItemMenu/windowItemCategory.js'

//-----------------------------------------------------------------------------
// Scene_Shop
//
// The scene class of the shop screen.



Scene_Shop.prototype.prepare = function(goods, purchaseOnly) {
    this._goods = goods;
    this._purchaseOnly = purchaseOnly;
    this._item = null;
};

Scene_Shop.prototype.create = function() {
    Scene_MenuBase.prototype.create.call(this);
    SoundManager.playOk();
    this.createInstructionWindow(); // 说明窗口
    this.createHelpWindow();        // 物品说明
    this.createCategoryWindow();    // 分类（卖时出现）
    this.createCommandWindow();     // 买/买/离开
    this.createNumberWindow();      // 购买数量
    this.createGoldWindow();        // 金钱窗口
    this.createStatusWindow();      // 状态（对比数字）/持有数
    this.createBuyWindow();         // 买物品列表
    this.createSellWindow();        // 卖物品列表
    


};

// 详细说明菜单
const _scene_shop_createHelpWindow = Scene_Item.prototype.createHelpWindow
Scene_Shop.prototype.createHelpWindow = function () {
    _scene_shop_createHelpWindow.call(this)
    this._helpWindow.x = 0;
    this._helpWindow.y = Graphics.boxHeight - this._helpWindow.height
    this._helpWindow.hide();
}


Scene_Shop.prototype.createGoldWindow = function() {
    this._goldWindow = new Window_Gold(0, 0);
    this._goldWindow.x = Graphics.boxWidth - this._goldWindow.width - 64;
    this._goldWindow.y = 40;

    this.addWindow(this._goldWindow);
};

Scene_Shop.prototype.createCommandWindow = function() {
    this._commandWindow = new SWDA_Window_ShopMenuCommand(this._purchaseOnly);
    this._commandWindow.setHandler('buy',    this.commandBuy.bind(this));
    this._commandWindow.setHandler('sell',   this.commandSell.bind(this));
    this._commandWindow.setHandler('cancel', this.popScene.bind(this));
    this.addWindow(this._commandWindow);
};


/*
Scene_Shop.prototype.createDummyWindow = function() {
    var wy = 128;
    var wh = Graphics.boxHeight - wy;
    this._dummyWindow = new Window_Base(0, wy, Graphics.boxWidth, wh);
    this.addWindow(this._dummyWindow);
};
*/
Scene_Shop.prototype.createNumberWindow = function() {
    //var wx = 0;
    //var wy = 440;
    //var ww = 380 - 32;
    //var wh = Graphics.boxHeight - wy - this._helpWindow.height;


    var wx = 380 + 482 + 16;
    var wy = this._categoryWindow.y + 65 + 60;
    var ww = Graphics.boxWidth - wx;
    var wh = 300;

    this._numberWindow = new Window_ShopNumber(wx, wy, ww, wh);
    this._numberWindow.hide();
    this._numberWindow.setHandler('ok',     this.onNumberOk.bind(this));
    this._numberWindow.setHandler('cancel', this.onNumberCancel.bind(this));
    this.addWindow(this._numberWindow);
};

Scene_Shop.prototype.createStatusWindow = function() {
    
    var wx = 380 + 482;
    //var wy = this._goldWindow.y + this._goldWindow.height;
    //var wy = 150;
    var wy = this._categoryWindow.y + 65 + 32 + 32 
    var ww = Graphics.boxWidth - wx;
    var wh = Graphics.boxHeight - wy - this._helpWindow.height;

    this._statusWindow = new Window_ShopStatus(wx, wy, ww, wh);
    this._statusWindow.hide();
    this.addWindow(this._statusWindow);
};


Scene_Shop.prototype.createCategoryWindow = function() {
    this._categoryWindow = new Local_Window_ItemCategory();
    this._categoryWindow.setHelpWindow(this._helpWindow);
    this._categoryWindow.y = 128;
    this._categoryWindow.hide();
    this._categoryWindow.deactivate();
    //this._categoryWindow.setHandler('ok',     this.onCategoryOk.bind(this));
    //this._categoryWindow.setHandler('cancel', this.onCategoryCancel.bind(this));
    this.addWindow(this._categoryWindow);
    
    // 设置外观基础值
    const [x, y, width] = [650-300, 40, 556];
    this._categoryWindow.x = x
    this._categoryWindow.y = y
    this._categoryWindow.width = width
    this._categoryWindow.setMaxCols(6);
    // 关联行为
    this._categoryWindow.filterRules = (item) => !['Weapons', 'Armors'].includes(item.trim())
    this._categoryWindow.refresh()
};


Scene_Shop.prototype.createBuyWindow = function() {
    const [x, y, width, height] = [680-300, this._categoryWindow.y + 65, 482, 480];
    this._buyWindow = new Window_ShopBuy(x, y, width, height, this._goods);

    //this._buyWindow = new Window_ItemList(x, y, width, height);
    //this._buyWindow.width = 482
    //this._buyWindow.opacity = 0
    this._buyWindow._scrollbar.setOffset(5);
    //this._categoryWindow.setItemWindow(this._itemWindow);
    //this.listenItemWindowEvents()
    //this.addWindow(this._itemWindow);
    //this._buyWindow.activate();
    //this._buyWindow.selectLast();


    this._buyWindow.setHelpWindow(this._helpWindow);
    this._buyWindow.setStatusWindow(this._statusWindow);
    this._buyWindow.hide();
    this._buyWindow.setHandler('ok',     this.onBuyOk.bind(this));
    this._buyWindow.setHandler('cancel', this.onBuyCancel.bind(this));
    this._buyWindow.$on('select',        this.updateBuyPreviewWindow.bind(this));
    this._buyWindow.$on('deselect',      this.hideBuyPreviewBox.bind(this));

    this.addWindow(this._buyWindow);
};

Scene_Shop.prototype.createSellWindow = function() {
    //var wy = this._categoryWindow.y + this._categoryWindow.height;
    //var wh = Graphics.boxHeight - wy;
    const [x, y, width, height] = [680-300, this._categoryWindow.y + 65, 482, 480];
    this._sellWindow = new Window_ShopSell(x, y, width, height);
    this._sellWindow._scrollbar.setOffset(5);
    this._sellWindow.setHelpWindow(this._helpWindow);
    this._sellWindow.hide();
    this._sellWindow.setHandler('ok',     this.onSellOk.bind(this));
    this._sellWindow.setHandler('cancel', this.onSellCancel.bind(this));
    this._sellWindow.$on('select',        this.updateSellPreviewWindow.bind(this));
    this._sellWindow.$on('deselect',      this.hideSellPreviewBox.bind(this));
    this._categoryWindow.setItemWindow(this._sellWindow);
    this.addWindow(this._sellWindow);
};

Scene_Shop.prototype.createInstructionWindow = function() {
    const tw = 13;
    const width = tw * Window_Base.prototype.standardFontSize() + Window_Base.prototype.textPadding() * 2 + Window_Base.prototype.standardPadding() * 2;
    const height = Window_Base.prototype.standardFontSize()  + Window_Base.prototype.textPadding() * 2 + Window_Base.prototype.standardPadding() * 2;
    const [x, y] = [0, 16];
    this._instructionWindow = new Window_Instruction(x, y, width, height);
    this.setInstructionDefault();
    this.addWindow(this._instructionWindow);
};

Scene_Shop.prototype.setInstructionDefault = function() {
    text = ""
    this._instructionWindow.setText(text);
}

Scene_Shop.prototype.activateBuyWindow = function() {
    this._buyWindow.setMoney(this.money());
    this._buyWindow.show();
    this._buyWindow.activate();
    this._statusWindow.show();
    this._commandWindow.hide();
    this._helpWindow.show();
};

Scene_Shop.prototype.activateSellWindow = function() {
    this._categoryWindow.show();
    //this._categoryWindow.activate();//会重置sell窗口光标，改用下面的
    this._categoryWindow.active = true;
    this._sellWindow.refresh();
    this._sellWindow.show();
    this._sellWindow.activate();
    //this._statusWindow.show();
    this._commandWindow.hide();
    this._helpWindow.show();
};

// 物品图
Scene_Shop.prototype.updateBuyPreviewWindow = function (id) {
    if (!this._buyWindow) return;
    this._buyWindow.showView()
}
Scene_Shop.prototype.hideBuyPreviewBox = function () {
    if (!this._itemWindow) return;
    this._buyWindow.hideView()
}

Scene_Shop.prototype.updateSellPreviewWindow = function (id) {
    if (!this._buyWindow) return;
    this._sellWindow.showView()
}
Scene_Shop.prototype.hideSellPreviewBox = function () {
    if (!this._itemWindow) return;
    this._sellWindow.hideView()
}

Scene_Shop.prototype.commandBuy = function() {
    //this._dummyWindow.hide();
    this._buyWindow.select(0);
    this.activateBuyWindow();
    this._instructionWindow.setText('请选择你要购买的物品。');
};

Scene_Shop.prototype.commandSell = function() {
    //this._dummyWindow.hide();
    this._categoryWindow.show();
    this._categoryWindow.activate();
    this._sellWindow.activate();// 会导致buy的number在cancel时无法隐藏(已修正)
    this._sellWindow.select(0);
    this._sellWindow.show();
    //this._sellWindow.deselect();
    this._sellWindow.refresh();
    this._commandWindow.hide();
    this._helpWindow.show();
    //this._statusWindow.show();
    this._instructionWindow.setText('请选择你要出售的物品。');
};

Scene_Shop.prototype.onBuyOk = function() {
    this._item = this._buyWindow.item();
    //this._buyWindow.hide();
    this._numberWindow.setup(this._item, this.maxBuy(), this.buyingPrice());
    this._numberWindow.setCurrencyUnit(this.currencyUnit());
    this._numberWindow.show();
    this._statusWindow.hide();
    this._numberWindow.activate();
};

Scene_Shop.prototype.onBuyCancel = function() {
    this._commandWindow.activate();
    //this._dummyWindow.show();
    this._buyWindow.deselect();
    this._buyWindow.hide();
    this._statusWindow.hide();
    this._statusWindow.setItem(null);
    this._helpWindow.clear();
    this._commandWindow.show();
    this._helpWindow.hide();
    this.setInstructionDefault();
};
/*
Scene_Shop.prototype.onCategoryOk = function() {
    this.activateSellWindow();
    this._sellWindow.select(0);
};
*/
/*
Scene_Shop.prototype.onCategoryCancel = function() {
    this._commandWindow.activate();
    //this._dummyWindow.show();
    this._categoryWindow.hide();
    this._sellWindow.hide();
    this._commandWindow.show();
    this._helpWindow.hide();
};
*/
Scene_Shop.prototype.onSellOk = function() {
    this._item = this._sellWindow.item();
    //this._categoryWindow.hide();
    //this._sellWindow.hide();
    this._categoryWindow.active = false;//如果用this._categoryWindow.deactivate();会重置sell的光标，但是去掉又会导致选择数字时仍可切换类型标签
    this._sellWindow.deactivate();
    this._numberWindow.setup(this._item, this.maxSell(), this.sellingPrice());
    this._numberWindow.setCurrencyUnit(this.currencyUnit());
    this._numberWindow.show();
    this._statusWindow.hide();
    this._numberWindow.activate();
    //this._statusWindow.setItem(this._item);
    //this._commandWindow.show();
    //this._helpWindow.hide();
};

Scene_Shop.prototype.onSellCancel = function() {
    this._sellWindow.deselect();
    //this._categoryWindow.activate();
    this._statusWindow.setItem(null);
    this._helpWindow.clear();

    //this.activateSellWindow();
    //this._sellWindow.select(0);

    this._commandWindow.activate();
    //this._dummyWindow.show();
    this._categoryWindow.hide();
    this._categoryWindow.deactivate();
    this._sellWindow.deselect();
    this._sellWindow.hide();
    this._commandWindow.show();
    this._helpWindow.hide();
    this.setInstructionDefault();
};

Scene_Shop.prototype.onNumberOk = function() {
    SoundManager.playShop();
    switch (this._commandWindow.currentSymbol()) {
    case 'buy':
        this.doBuy(this._numberWindow.number());
        break;
    case 'sell':
        this.doSell(this._numberWindow.number());
        break;
    }
    this.endNumberInput();
    this._goldWindow.refresh();
    this._statusWindow.refresh();
};

Scene_Shop.prototype.onNumberCancel = function() {
    SoundManager.playCancel();
    this.endNumberInput();
};

Scene_Shop.prototype.doBuy = function(number) {
    $gameParty.loseGold(number * this.buyingPrice());
    $gameParty.gainItem(this._item, number);
};

Scene_Shop.prototype.doSell = function(number) {
    $gameParty.gainGold(number * this.sellingPrice());
    $gameParty.loseItem(this._item, number);
};

Scene_Shop.prototype.endNumberInput = function() {
    this._numberWindow.hide();
    switch (this._commandWindow.currentSymbol()) {
    case 'buy':
        this._statusWindow.show();
        this.activateBuyWindow();
        break;
    case 'sell':
        this.activateSellWindow();
        break;
    }
};

Scene_Shop.prototype.maxBuy = function() {
    var max = $gameParty.maxItems(this._item) - $gameParty.numItems(this._item);
    var price = this.buyingPrice();
    if (price > 0) {
        return Math.min(max, Math.floor(this.money() / price));
    } else {
        return max;
    }
};

Scene_Shop.prototype.maxSell = function() {
    return $gameParty.numItems(this._item);
};

Scene_Shop.prototype.money = function() {
    return this._goldWindow.value();
};

Scene_Shop.prototype.currencyUnit = function() {
    return this._goldWindow.currencyUnit();
};

Scene_Shop.prototype.buyingPrice = function() {
    return this._buyWindow.price(this._item);
};

Scene_Shop.prototype.sellingPrice = function() {
    return Math.floor(this._item.price / 2);
};



//=============================================================================
// Scene_Shop
//=============================================================================

Yanfly.Item.Scene_Shop_doBuy = Scene_Shop.prototype.doBuy;
Scene_Shop.prototype.doBuy = function(number) {
    $gameTemp.enableVarianceStock();
    Yanfly.Item.Scene_Shop_doBuy.call(this, number);
    $gameTemp.disableVarianceStock();
};

Yanfly.Item.Scene_Shop_doSell = Scene_Shop.prototype.doSell;
Scene_Shop.prototype.doSell = function(number) {
    Yanfly.Item.Scene_Shop_doSell.call(this, number);
    if (!DataManager.isIndependent(this._item)) return;
    DataManager.removeIndependentItem(this._item);
};
